package com.get.backstage.user.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.get.backstage.common.result.R;
import com.get.backstage.user.entity.User;
import com.get.backstage.user.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Optional;

/**
 * 用户信息表(User)表控制层
 *
 * @author fanjia
 * @since 2021-09-29 16:16:19
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PasswordEncoder passwordEncoder;


    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "用户的分页查询功能", notes = "分页参数传入current和size,用户条件参数传入username和phone和email")
    @GetMapping("/page")
    public R selectAll(@ApiParam(value = "分页信息")Page<User> page, @ApiParam(value = "单个用户信息") User user) {
//        int i = 11/0;
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>()
                .like(StrUtil.isNotBlank(user.getUsername()), "username", user.getUsername())
                .like(StrUtil.isNotBlank(user.getPhone()), "phone", user.getPhone())
                .like(StrUtil.isNotBlank(user.getEmail()), "email", user.getEmail());
        IPage<User> userPage = userService.page(page, userQueryWrapper);

        return R.success(userPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param userCode 主键
     * @return 单条数据
     */
    @GetMapping("info/{userCode}")
    @ApiOperation(value = "根据id获取用户信息", notes = "userCode必须要有值")
    public R selectOne(@PathVariable @ApiParam(value = "单个用户信息") Optional<String> userCode) {
        User user = userService.getOne(new QueryWrapper<User>().eq("user_code", userCode.get()));
        return R.success(user);
    }

    /**
     * 新增数据
     *
     * @param user 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增用户", notes = "新增用户")
    @Transactional(rollbackFor = Exception.class)
    public R insert(@ApiParam(value = "单个用户信息") @RequestBody User user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return R.success(this.userService.save(user));
    }

    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新用户", notes = "根据userCode更新")
    public R update(@ApiParam(value = "单个用户信息") @RequestBody User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_code", user.getUserCode());
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return R.success(this.userService.update(user, updateWrapper));
    }

    /**
     * 删除数据
     *
     * @param userCodeList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "批量删除用户", notes = "逻辑删除")
    public R delete(@RequestParam("userCodeList") @ApiParam(value = "userCode的List") List<String> userCodeList) {
        userCodeList.stream().forEach(userCode -> {
            userService.remove(new UpdateWrapper<User>().eq(StrUtil.isNotBlank(userCode), "user_code", userCode));
        });
        return R.success("删除成功");
    }
}